package com.shensi.adminCollect.service.station.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shensi.adminCollect.domain.station.AppUserSuperDetail;
import com.shensi.adminCollect.domain.station.Enterprise ;
import com.shensi.adminCollect.domain.station.SuperPayOrder;
import com.shensi.adminCollect.mapper.station.AppUserSuperDetailMapper;
import com.shensi.adminCollect.service.station.IAppUserSuperDetailService;
import com.shensi.adminCollect.service.station.IEnterpriseService;
import com.shensi.adminCollect.service.station.ISuperPayOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author qujia
 * @date 2021-11-30
 */
@Service
public class AppUserSuperDetailServiceImpl implements IAppUserSuperDetailService
{
    @Autowired
    private AppUserSuperDetailMapper appUserSuperDetailMapper;

    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private ISuperPayOrderService superPayOrderService;

    /**
     * 查询【请填写功能名称】
     * 
     * @param userId 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public AppUserSuperDetail selectAppUserSuperDetailById(Long userId)
    {
        return appUserSuperDetailMapper.selectAppUserSuperDetailById(userId);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param appUserSuperDetail 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<AppUserSuperDetail> selectAppUserSuperDetailList(AppUserSuperDetail appUserSuperDetail)
    {
        return appUserSuperDetailMapper.selectAppUserSuperDetailList(appUserSuperDetail);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param appUserSuperDetail 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertAppUserSuperDetail(AppUserSuperDetail appUserSuperDetail)
    {
        return appUserSuperDetailMapper.insertAppUserSuperDetail(appUserSuperDetail);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param appUserSuperDetail 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateAppUserSuperDetail(AppUserSuperDetail appUserSuperDetail)
    {
        return appUserSuperDetailMapper.updateAppUserSuperDetail(appUserSuperDetail);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param userIds 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteAppUserSuperDetailByIds(Long[] userIds)
    {
        return appUserSuperDetailMapper.deleteAppUserSuperDetailByIds(userIds);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param userId 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteAppUserSuperDetailById(Long userId)
    {
        return appUserSuperDetailMapper.deleteAppUserSuperDetailById(userId);
    }
    @Override
    public List<AppUserSuperDetail> selectList(AppUserSuperDetail appUserSuperDetail)
    {
        List<AppUserSuperDetail> appUserSuperDetails = appUserSuperDetailMapper.selectList(appUserSuperDetail);
        for (AppUserSuperDetail userSuperDetail : appUserSuperDetails) {
            if(Objects.isNull(userSuperDetail) || Objects.isNull(userSuperDetail.getEnterpriseId())) {
                continue;
            }
            Enterprise enterprise = enterpriseService.selectEnterpriseById(userSuperDetail.getEnterpriseId());
            if(Objects.nonNull(enterprise) && StringUtils.isNotEmpty(enterprise.getEnterpriseName())){
                userSuperDetail.setEnterpriseName(enterprise.getEnterpriseName());
            }
            if (StringUtils.isNotBlank(userSuperDetail.getPayOrder())) {
                SuperPayOrder form = new SuperPayOrder();
                form.setSuperOrderNo(userSuperDetail.getPayOrder());
                QueryWrapper<SuperPayOrder> qw = new QueryWrapper<>(form);
                qw.last("limit 1");
                SuperPayOrder one = superPayOrderService.getOne(qw);
                if (one != null) {
                    userSuperDetail.setAmt(one.getAmt());
                }
            }
        }
        return appUserSuperDetails;
    }
}
